package com.example.farmer.Fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toolbar;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;
import androidx.viewpager2.widget.ViewPager2;

import com.example.farmer.Activity.MineCircleActivity;
import com.example.farmer.Activity.PostCircleActivity;
import com.example.farmer.Adapter.TabsPagerAdapter;
import com.example.farmer.Presenter.CirclePresenter;
import com.example.farmer.R;
import com.example.farmer.View.CircleView;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;

public class CircleFragment extends Fragment implements CircleView {

    private CirclePresenter mPresenter;
    private TextView textView;
    private TabLayout tabLayout;
    private ViewPager2 viewPager;
    private Button mMyButton;
    private TabsPagerAdapter tabsPagerAdapter;
    private ImageButton mImageButton;



    public CircleFragment() {
        // Required empty public constructor
    }

    public static CircleFragment newInstance(String param1, String param2) {
        CircleFragment fragment = new CircleFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.activity_circle, container, false);

        textView = view.findViewById(R.id.toolbar_title);
        mMyButton = view.findViewById(R.id.my_button);
        tabLayout = view.findViewById(R.id.tabs);
        viewPager = view.findViewById(R.id.view_pager);
        mImageButton = view.findViewById(R.id.issue);
        tabsPagerAdapter = new TabsPagerAdapter(this);
        viewPager.setAdapter(tabsPagerAdapter);
        mPresenter = new CirclePresenter(this);

        // 初始化数据
        initData();
        textView.setText("海报圈");

        // 设置TabLayout与ViewPager2关联
        tabLayout.addTab(tabLayout.newTab().setText("最新"));
        tabLayout.addTab(tabLayout.newTab().setText("关注"));

        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                int position = tab.getPosition();
                viewPager.setCurrentItem(position);
                mPresenter.onTabSelected(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                // 可以不实现
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
                // 可以不实现
            }
        });

        viewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                tabLayout.getTabAt(position).select();
            }
        });
        mMyButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPresenter.onMyButtonClicked();
            }
        });

        mImageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), PostCircleActivity.class);
                startActivity(intent);
            }
        });

        return view;
    }


    private void initData(){
        tabsPagerAdapter.setLatestList(getSampleList());
        tabsPagerAdapter.setFollowList(getSampleList());
    }

    private List<String> getSampleList() {
        List<String> list = new ArrayList<>();
        for (int i = 1; i <= 10; i++) {
            list.add("Item " + i);
        }
        return list;
    }

    // 实现ClassView接口方法
    @Override
    public void showLatest(List<String> latestList) {
        tabsPagerAdapter.setLatestList(latestList);
        viewPager.setCurrentItem(0);
    }

    @Override
    public void showFollow(List<String> followList) {
        tabsPagerAdapter.setFollowList(followList);
        viewPager.setCurrentItem(1);
    }

    @Override
    public void setPresenter(CirclePresenter circlePresenter) {
        this.mPresenter = circlePresenter;
    }

    @Override
    public void navigateToMyProfile() {
        // 实现跳转到我的页面的逻辑
        Intent intent = new Intent(getActivity(), MineCircleActivity.class);
        startActivity(intent);
    }
}